
__aof__ = ('ATree Text Exporter', 'ATE')
__version__ = '0.1'
__extension__ = (('Text Files', ('*.txt',),),)

import os
import re


import ab_utils

from ab_field import AtrField
from ab_object import AtrObject


def _dumpTree(target, objt, safestr, indent=''):
    subject, content = objt.extract('sc')

    li = indent + safestr(subject)
    if content:
        li += ': %s' % safestr(content)

    li += '\n'
    target.write(li)

    if objt.hasChildren():
        for ee in objt.getChildren():
            _dumpTree(target, ee, safestr, indent=indent + '  ')


class _AtrExporter:
    def Execute(self, source, target, notify, **kws):
        def safestr(str_):
            return str_

        tree = kws.get('tree')
        if tree is not None:
            if '_i' in kws:
                safestr = kws['_i'].safestr

            _dumpTree(target, tree, safestr)
            return True

        return False


def genService():
    return _AtrExporter
